package cate.game.play.buff.h;

import cate.common.table.d.GD;
import cate.common.table.d.GDSkill;
import cate.common.util.XT;
import cate.game.play.base.TargetFilterParam;
import cate.game.play.base.TargetSelector;
import cate.game.play.buff.BuffHandler;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import cate.game.play.proce.action.ctx.ActionCtx;
import cate.game.play.skill.FighterCD;
import cate.game.play.util.FighterFilter;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 每回合根据条件加buff
 */
public class 回合添加buffBH extends BuffHandler {

	private List<Config> configs =new ArrayList<>();

	@Override
	public void parse(String code) {
		if(StringUtils.isNotBlank(code)){
			for (String s : code.split(",")) {
				configs.add(new Config(s));
			}
		}
	}

	@Override
	public void onRoundBegin(ActionCtx action) {
		for (Config config : configs) {
			config.settle(action, buff.owner);
		}
	}

	public static class Config{

		private PlayCfgArgs args;

		private List<Integer> buff;

		private int 数量;

		private byte 敌我目标;

		private Integer 排序属性;

		private int 人数;

		private int 概率;

		private int 开始回合;

		private int 结束回合;

		private int 间隔回合;

		//buff=??#??&数量=1&敌我目标=2&排序属性=?&人数=1&概率=10000&
		//开始回合=？？&结束回合=？？&间隔回合=1
		public void parse(String code){
			args = new PlayCfgArgs(code);
			buff = args.getIntList("buff");
			数量 = args.getInt("数量", 1);
			敌我目标 = args.getByte("敌我目标", GDSkill.Scope.SELF);
			排序属性 = args.getInt("排序属性", null);
			人数 = args.getInt("人数",1);
			概率 = args.getInt("概率", GD.W_10000);
			开始回合 = args.getInt("结束回合", -1);
			结束回合 = args.getInt("结束回合",-1);
			间隔回合 = args.getInt("间隔回合",-1);
		}

		private int lastRound;

		public Config(String code) {
			parse(code);
		}

		public void settle(ActionCtx action, Fighter owner) {
			if (XT.isBlank(buff)) {
				return;
			}
			if (开始回合 > 0 && 开始回合 > action.getRound().index) {
				return;
			}
			if (结束回合 > 0 && 结束回合 < action.getRound().index) {
				return;
			}
			if (FighterCD.inCD(lastRound, 间隔回合, action.getRound().index)) {
				return;
			}
			lastRound = action.getRound().index;
			TargetFilterParam param = new TargetFilterParam();
			param.scopeSet(敌我目标).targetNumSet(人数);
			if (排序属性 != null) {
				param.bigAttrSet(排序属性);
			}
			for (Fighter target : TargetSelector.doFind(owner, action.getTeamVs(), param)) {
				if (FighterFilter.test(args, owner, target)) {
					for (Integer integer : buff) {
						target.buff.tryAddSome(action, owner, integer, 数量, 概率);
					}
				}
			}
		}
	}
}
